/**
 * Created by mingtao on 7/23/16.
 */
var $table = $('#table'),
  $remove = $('#remove'),
  selections = [];

function initTable() {
  $table = $('#table');
  $remove = $('#remove');
  $table.bootstrapTable({
    formatLoadingMessage: function () {
      return '正在努力地加载数据中，请稍候……';
    },
    formatRecordsPerPage: function (pageNumber) {
      return '每页显示 ' + pageNumber + ' 条记录';
    },
    formatShowingRows: function (pageFrom, pageTo, totalRows) {
      return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
    },
    formatSearch: function () {
      return '搜索';
    },
    formatNoMatches: function () {
      return '没有找到匹配的记录';
    },
    formatPaginationSwitch: function () {
      return '隐藏/显示分页';
    },
    formatRefresh: function () {
      return '刷新';
    },
    formatToggle: function () {
      return '切换';
    },
    formatColumns: function () {
      return '列';
    },
    height: getHeight(),
    columns: [
      [
        {
          field: 'state',
          checkbox: true,
          align: 'center',
        },
        {
          field: 'content',
          title: '反馈内容',
          align: 'center',
          formatter: function (value) {
            return "<a href='javascript: void 0' class='title'>" + value + "</a>";
          },
          events: operateEvents
        },
        {
          field: 'from',
          title: '反馈人',
          align: 'center',
        },
        {
          field: 'time',
          title: '反馈时间',
          align: 'center',
        },
        {
          field: 'status',
          title: '状态',
          align: 'center'
        }
      ]
    ],
    data: [
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
      {
        content: "这是一条反馈内容",
        from: "李四",
        time: "2016-01-01 11:11:11",
        status: "未读"
      },
    ]
  });
  // sometimes footer render error.
  setTimeout(function () {
    $table.bootstrapTable('resetView');
  }, 200);
  $table.on('check.bs.table uncheck.bs.table ' +
    'check-all.bs.table uncheck-all.bs.table', function () {
    $remove.prop('disabled', !$table.bootstrapTable('getSelections').length);

    // save your data, here just save the current page
    selections = getIdSelections();
    // push or splice the selections if you want to save all data selections
  });
  $table.on('expand-row.bs.table', function (e, index, row, $detail) {
    if (index % 2 == 1) {
      $detail.html('Loading from ajax request...');
      $.get('LICENSE', function (res) {
        $detail.html(res.replace(/\n/g, '<br>'));
      });
    }
  });
  $table.on('all.bs.table', function (e, name, args) {
    //console.log(e, name, args);
  });
  $table.on('click-cell.bs.table', function (e, name, args) {
    console.log(e, name, args);
  });
  $table.on('click-row.bs.table', function (e, row, $ele) {
    console.log($ele)
  });
   $remove.click(function () {
   $("#confirm_modal").modal('show');
   //$remove.prop('disabled', true);
   });
  $(window).resize(function () {
    $table.bootstrapTable('resetView', {
      height: getHeight()
    });
  });
  $('.fixed-table-container').addClass('box box-primary');
}

function getIdSelections() {
  return $.map($table.bootstrapTable('getSelections'), function (row) {
    return row.id
  });
}

var func_confirm = function() {};

window.operateEvents = {
  'click .title': function (e, value, row, index) {
    $('#feedback_modal').modal('show');
  }
};

function getHeight() {
  return $("#right").height();
}

$(function () {
  var scripts = [
      './scripts/vendors/bootstrap-table.js',
      //'./static/js/tableExport.js',
      //'./static/js/bootstrap-table-editable.js',
      //'./static/js/bootstrap-editable/bootstrap-editable.js'
    ],
    eachSeries = function (arr, iterator, callback) {
      callback = callback || function () {};
      if (!arr.length) {
        return callback();
      }
      var completed = 0;
      var iterate = function () {
        iterator(arr[completed], function (err) {
          if (err) {
            callback(err);
            callback = function () {};
          }
          else {
            completed += 1;
            if (completed >= arr.length) {
              callback(null);
            }
            else {
              iterate();
            }
          }
        });
      };
      iterate();
    };

  eachSeries(scripts, getScript, initTable);
});

function getScript(url, callback) {
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.src = url;

  var done = false;
  // Attach handlers for all browsers
  script.onload = script.onreadystatechange = function() {
    if (!done && (!this.readyState ||
      this.readyState == 'loaded' || this.readyState == 'complete')) {
      done = true;
      if (callback)
        callback();

      // Handle memory leak in IE
      script.onload = script.onreadystatechange = null;
    }
  };

  head.appendChild(script);

  // We handle everything using the script element injection
  return undefined;
}

